package org.example.ctsbocc.controller;

import jakarta.annotation.Resource;
import org.example.ctsbocc.entity.User;
import org.example.ctsbocc.service.UserService;
import org.example.ctsbocc.utils.LoggerUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/registerController")
public class RegisterController {

    @Resource(name = "userService")
    private UserService userService;

    @PostMapping("/register")
    public boolean register(@RequestBody User user) {
        // 参数校验
        if (user == null) {
            LoggerUtil.log().warn("注册失败：用户信息为空");
            return false;
        }

        // 测试专用
//        LoggerUtil.log().info("用户 {} 注册开始", user.getGitUsername());
//        System.out.println( user);
//        return true;

        try {
            if (userService.register(user) == 1) {
                LoggerUtil.log().info("用户 {} 注册成功", user.getGitUsername());
                return true;
            }
        } catch (Exception e) {
            LoggerUtil.log().error("注册失败，用户名：{}", user.getGitUsername() != null ? user.getGitUsername() : "未知", e);
        }
        return false;
    }

}
